<?php
/*
 * @Author: 李明
 * @Date: 2021-11-08 16:08:05
 * @LastEditTime: 2021-11-30 18:05:12
 * @LastEditors: 李明
 * @Description: 老订单接口
 * @FilePath: /CRM2.0-PHP/app/Http/Resources/Order.php
 * @山东壹零贰肆信息科技有限公司
 */

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Order extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $time_unit = $this->product->time_unit ?? '';
        $long_time =  $this->product->long_time ?? '';
        switch ($this->pay_type) { // 付款方式付款方式  2自然月  4到期 5不付款 4自定义
            case '5':
                $order['product_long_time'] = '无需支付';
            default:
                switch ($time_unit) { // 付款间隔 1日、 2月、3年
                    case '1':
                        $order['product_long_time'] = $long_time . '日';
                        break;
                    case '2':
                        $order['product_long_time'] = $long_time . '个月';
                        break;
                    case '3':
                        $order['product_long_time'] = $long_time . '年';
                        break;
                    default:
                        $order['product_long_time'] = '';
                        break;
                }
        }
        // 返回需要使用的参数
        $order['user_id'] =  $this->user->id ?? '';
        $order['name'] =  $this->user->name ?? '';
        $order['worker_id'] = $this->worker->id ?? '';
        $order['worker_name'] = $this->worker->name ?? '';
        $order['bank_card_id'] = $this->bank_card->id ?? '';
        $order['bank_sum_name'] = $this->bank_card->bank_sum_name ?? '';
        $order['bank_num'] = $this->bank_card->bank_num ?? '';
        $order['account_name'] = $this->bank_card->account_name ?? '';
        $order['bank_id'] =  $this->bank_card->bank->id ?? '';
        $order['bank_name'] =  $this->bank_card->bank->bank_name ?? '';
        $order['branch_id'] = $this->branch->id;
        $order['branch_name'] = $this->branch->name;
        $order['product_id'] = $this->product->id ?? '未知';
        $order['product_name'] = $this->product->name ?? '未知';
        $order['long_time'] = $this->product->long_time ?? '未知';
        $order['time_unit'] = $this->product->time_unit ?? '未知';
        $order['pay_type'] = $this->product->pay_type ?? '未知';
        $order['pay_long'] = $this->product->pay_long ?? '未知';
        $order['pay_time_unit'] = $this->product->pay_time_unit ?? '未知';
        $order['year_type'] = $this->product->year_type ?? '未知';
        $order['product_money_start'] = $this->product->money ?? '未知';
        $order['product_money_end'] = $this->product->ceiling ?? '未知';
        $order['product_rate'] = $this->product->rate ?? '未知';
        $order['results_rate'] = $this->product->results_rate ?? '未知';
        $order['class_id'] = $this->product->product_class->id ?? '未知';
        $order['category_name'] = $this->product->product_class->category_name ?? '未知';
        $order['to_account'] = $this->working_family->open_bank ?? '未知';
        $order_records = $this->order_records->where('type', 1)->sortByDesc('update_time')->first();
        $order['reason'] = $order_records->remark ?? '';
        $order['order_id'] = $this->id;
        $order['order_no'] = $this->order_no;
        $order['valid_time'] = date('Y-m-d', strtotime($this->valid_time));
        $order['sign_time'] = date('Y-m-d', strtotime($this->sign_time));
        if ($this->account_time) {
            $order['account_time'] = date('Y-m-d', strtotime($this->account_time));
        } else {
            $order['account_time'] = null;
        }
        $order['contract_no'] = $this->contract_no;
        $order['order_money'] = $this->order_money;
        $order['results_money'] = $this->results_money;
        $order['investment_way'] = $this->investment_way;
        $order['is_first'] = $this->is_first;
        $order['order_status'] = $this->order_status;
        if ($this->contract_end_date) {
            $order['contract_end_date'] = date('Y-m-d', strtotime($this->contract_end_date));
        } else {
            $order['contract_end_date'] = null;
        }
        $order['families_money_records'] = $this->families_money_records;
        $order['course_money'] = $this->course_money;
        // pay_images 支付凭证  -1未知 1身份证正2身份证反3转款凭证4合同照片   5: '银行卡正面',6: '银行卡反面',7: '其他'
        $order['other_images'] = $this->images->where('img_type', '7')->all();
        $order['ht_images'] = $this->images->where('img_type', '4')->all();
        $order['pay_images'] = $this->images->where('img_type', '3')->all();
        $order['update_time'] = date('Y-m-d H:i:s', strtotime($this->update_time));
        $order['create_time'] = date('Y-m-d H:i:s', strtotime($this->create_time));
        $order['remark'] = $this->remark;

        return $order;
    }
}
